using System.Data;
using Framework.Library.Components.Providers;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using System;
using System.Web.UI.WebControls;

namespace Framework.Library.Components.Users
{
	public class UserController
	{
        private const string key = "Framework.Portal.User";
		public static UserInfo GetCurrentUser()
		{
			return DataAccessProvider.Instance().GetUser(AuthenticateUtility.GetUsername());
		}
		public static DataTable GetUsers()
		{
			//string param = "GetUsers";
            //DataCaching data = new DataCaching();
            DataTable dtUsers ;//= (DataTable) data.GetHashCache(key, param);
            //if (dtUsers != null) return dtUsers;
			dtUsers = DataAccessProvider.Instance().GetUsers();
			//data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
			return dtUsers;
		}
       
		public static DataTable GetUsers(int roleId, int portalId)
		{
            //string param = "GetUsers?roleid=" + roleId + "&portalId=" + portalId;
			//DataCaching data = new DataCaching();
			DataTable dtUsers;// = (DataTable)data.GetHashCache(key, param);
			//if (dtUsers != null) return dtUsers;
            dtUsers = DataAccessProvider.Instance().GetUsers(roleId, portalId);
			//data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
			return dtUsers;
		}
		public static DataTable GetUsers(string keyword, string searchby, string filter, int portalid)
		{
            return DataAccessProvider.Instance().GetUsers(keyword, searchby, filter, portalid);
		}
        public static DataTable GetUsersByUser(string keyword, string searchby, string filter, int userid, int portalid)
        {
            return DataAccessProvider.Instance().GetUsersByUser(keyword, searchby, filter, userid, portalid);
        }
		public static DataTable GetUsersNotInRole(int roleId, int portalId)
		{
			//string param = "GetUsersNotInRole?roleid=" + roleId + "&portalid=" + portalId;
			//DataCaching data = new DataCaching();
			DataTable dtUsers ;//= (DataTable)data.GetHashCache(key, param);
			//if (dtUsers != null) return dtUsers;
			dtUsers = DataAccessProvider.Instance().GetUsersNotInRole(roleId, portalId);
			//data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtUsers);
			return dtUsers;
		}
		public static UserInfo ValidateUser(string username, string password)
		{
			return DataAccessProvider.Instance().ValidateUser(username, password);
		}

		public static int AddUser(UserInfo user)
		{
			int id = DataAccessProvider.Instance().AddUser(user);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
			return id;
		}
		public static void UpdateUser(UserInfo user)
		{
			DataAccessProvider.Instance().UpdateUser(user);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
		}
        public static void EditUser(UserInfo user)
		{
            DataAccessProvider.Instance().EditUser(user);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
		}        
		public static void DeleteUser(int userId)
		{
			DataAccessProvider.Instance().DeleteUser(userId);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
		}
		public static UserInfo GetUser(int userId)
		{
			return DataAccessProvider.Instance().GetUser(userId);
		}
		public static UserInfo GetUser(string username)
		{
			return DataAccessProvider.Instance().GetUser(username);
		}
		public static void AddCommandToUser(int commandId, int userId)
		{
			DataAccessProvider.Instance().AddCommandToUser(commandId, userId);
			DataCaching data = new DataCaching();
            data.RemoveCache("Framework.Portal.Command.GetCommandsForUserNotGroup");
		}
		public static void RemoveCommandFromUser(int commandId, int userId)
		{
			DataAccessProvider.Instance().RemoveCommandFromUser(commandId, userId);		
		}
		public static void ResetPassword(int userId, string password)
		{
			DataAccessProvider.Instance().ResetPassword(userId, password);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
		}
		public static void ChangePassword(int userId, string oldPassword, string newPassword)
		{
			DataAccessProvider.Instance().ChangePassword(userId, oldPassword, newPassword);
			DataCaching data = new DataCaching();
			data.RemoveCache(key);
		}
        public static void AddCommandToUserByPortalID(int commandId, int userId, int portalId)
        {
            DataAccessProvider.Instance().AddCommandToUserByPortalID(commandId, userId, portalId);
            DataCaching data = new DataCaching();
            data.RemoveCache("Framework.Portal.Command.GetCommandsForUserNotGroup");
        }
        public static void RemoveCommandFromUserByPortalID(int commandId, int userId, int portalId)
        {
            DataAccessProvider.Instance().RemoveCommandFromUserByPortalID(commandId, userId, portalId);
        }
        public static DataRow[] GetUserPermissionByParentID(DataTable dtCommand, int parentID)
        {
            DataRow[] retVal = null;
            retVal = dtCommand.Select("CommandParentID = " + parentID);
            return retVal;
        }
        public static DataTable BuildRecursiveUserPermission(DataTable dtCommand, DataTable dtReturnCommand,int userid, int portalid)
        {
            DataRow[] dtItems = GetUserPermissionByParentID(dtCommand, 0);
            foreach (DataRow row in dtItems)
            {
                DataRow rowItem = dtReturnCommand.NewRow();
                rowItem["CommandID"]    = row["CommandID"].ToString();
                rowItem["CommandKey"] = row["CommandKey"].ToString();
                rowItem["PortalID"] = portalid.ToString();
                rowItem["CommandParentID"] = row["CommandParentID"].ToString();
                rowItem["UserID"] = userid.ToString();
                rowItem["CommandName"]  = "<b>" + row["CommandName"].ToString() + "</b>";
                rowItem["Role_View"] = row["Role_View"].ToString();
                rowItem["Role_Add"] = row["Role_Add"].ToString();
                rowItem["Role_Update"] = row["Role_Update"].ToString();
                rowItem["Role_Delete"] = row["Role_Delete"].ToString();
                rowItem["Level"] = "0";

                dtReturnCommand.Rows.Add(rowItem);
                LoadUserPermissionForCur(dtCommand, rowItem, dtReturnCommand);
            }
            DataCaching data = new DataCaching();
            data.RemoveCache("Framework.Portal.Command.GetCommandsForUserNotGroup");
            return dtReturnCommand;
        }
        public static DataTable LoadUserPermissionForCur(DataTable dtCommand,DataRow curItem, DataTable dtReturnCommand)
        {
            int level = Convert.ToInt32(curItem["Level"]);
            level += 1;
            int _commandID = Convert.ToInt32(curItem["CommandID"]);
            DataRow[] dtChild = GetUserPermissionByParentID(dtCommand, _commandID);
            foreach (DataRow row in dtChild)
            {
                DataRow childItem = dtReturnCommand.NewRow();
                childItem["CommandID"] = row["CommandID"].ToString();
                childItem["CommandKey"] = row["CommandKey"].ToString();
                childItem["CommandParentID"] = row["CommandParentID"].ToString();
                childItem["PortalID"] = row["PortalID"].ToString();
                childItem["UserID"] = row["UserID"].ToString();
                childItem["CommandName"] = MiscUtility.StringIndent(level) + row["CommandName"].ToString();               
                childItem["Role_View"] = row["Role_View"].ToString();
                childItem["Role_Add"] = row["Role_Add"].ToString();
                childItem["Role_Update"] = row["Role_Update"].ToString();
                childItem["Role_Delete"] = row["Role_Delete"].ToString();
                childItem["Level"] = level.ToString();

                dtReturnCommand.Rows.Add(childItem);
                LoadUserPermissionForCur(dtCommand, childItem, dtReturnCommand);
            }
            return dtReturnCommand;
        }
        public static void UpdateUserPermissionByPortalID(int userId,int commandId, int portalId, bool view, bool add, bool update, bool delete)
        {
            //DataCaching data = new DataCaching();
            //data.RemoveCache("Framework.Portal.Command.GetCommandsForUserNotGroup");
            DataAccessProvider.Instance().UpdateUserPermissionByPortalID(commandId, userId, portalId, view,  add,  update,  delete);           
        }
        public static bool IsApproved(string strUsername)
        {
            bool User = DataAccessProvider.Instance().IsApproved(strUsername);
            return User;
        }
        public static bool IsSuspended(string strUsername)
        {
            bool User = DataAccessProvider.Instance().IsSuspended(strUsername);
            return User;
        }

        public static void LoadUser(ListItemCollection lstItems, int PortalID)
        {
            DataTable dtUsers = DataAccessProvider.Instance().GetAllByPortal(PortalID);
            foreach (DataRow row in dtUsers.Rows)
            {
                ListItem item = new ListItem(row["Username"].ToString(), row["UserID"].ToString());
                item.Attributes.Add("Level", "0");
                lstItems.Add(item);
            }
        }
	}
	
}
